package com.larry.mr.reducejoin;

import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName hadoop
 * @Author larry
 * @Date 15:37 2021/11/16
 * @Description TODO
 **/
public class ReduceJoinReducer extends Reducer<Text,OrderPd,OrderPd, NullWritable> {

    private List<OrderPd> list = new ArrayList<OrderPd>();
    private OrderPd orderPd = new OrderPd();

    @SneakyThrows
    @Override
    protected void reduce(Text key, Iterable<OrderPd> values, Context context) throws IOException, InterruptedException {
        for (OrderPd value : values) {
            if (value.getFlag().equals("order")){
                OrderPd pd = new OrderPd();
                BeanUtils.copyProperties(pd,value);
                list.add(pd);
            }else {
                BeanUtils.copyProperties(orderPd,value);
            }
        }


        //join操作
        for (OrderPd od : list) {
            od.setPname(orderPd.getPname());
            context.write(od,NullWritable.get());
        }
        list.clear();
    }
}
